package com.wsw.common.model;


import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 
 * File：QueryResult.java 
 * title：分页工具类
 * Description:
 * Copyright: Copyright (c) 2013
 *
 * @author sw.wang
 * @version
 */
public class QueryResult<T> {
	private static final int PAGE_SIZE = 20;
	/**
	 * 页面大小
	 */
	private int pageSize = PAGE_SIZE;
	/**
	 * 起始位置
	 */
	private int start;
	/**
	 * 存放的记录
	 */
	private List<T> data;
	/**
	 * 总记录大小
	 */
	private int totalCount;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	/**
	 * 用来构造空页
	 */
	public QueryResult() {
		this(0, 0, PAGE_SIZE, new ArrayList<T>());
	}

	/**
	 * 默认构造方法
	 * 
	 * @param start
	 * @param totalCount
	 * @param pageSize
	 * @param data
	 */
	public QueryResult(int start, int totalCount, int pageSize, List<T> data) {
		this.start = start;
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		this.data = data;
	}

	/**
	 * 获取总页数
	 * 
	 * @return
	 */
	public int getTotalPageCount() {
		if (totalCount % pageSize == 0) {
			return totalCount / pageSize;
		} else {
			return totalCount / pageSize + 1;
		}
	}

	/**
	 * 获取当前页码，从1开始
	 * 
	 * @return
	 */
	public int getCurrentPageNo() {
		return start / pageSize + 1;
	}

	/**
	 * 判断是否还有下一页
	 * 
	 * @return
	 */
	public boolean isHasNextPage() {
		return this.getCurrentPageNo() < this.getTotalPageCount();
	}

	/**
	 * 判断是否还有上一页
	 * 
	 * @return
	 */
	public boolean isHasPreviousPage() {
		return this.getCurrentPageNo() > 1;
	}

	/**
	 * 使用默认页面大小
	 * 
	 * @param pageNo
	 * @return
	 */
	protected static int getStartOfPage(int pageNo) {
		return getStartOfPage(pageNo, PAGE_SIZE);
	}

	/**
	 * 获取任一页第一条数据在集合中的位置
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public static int getStartOfPage(int pageNo, int pageSize) {
		if(pageSize == 0){
			return getStartOfPage(pageNo);
		}else{
			return (pageNo - 1) * pageSize;
		}
	}
}
